package soen343.data;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.dsrg.ts.threadLocal.DbRegistry;

public class QuestionTDG {
	private QuestionTDG() { }
	
	public static ResultSet findById(int id) throws SQLException{
		String SQLString = 	"SELECT q.id as questionid, quizid, type, correct_choice_id, name, q.text, t.text as choice_text , lowerbound, upperbound "+
										" FROM `question` q left outer join numeric_choice n on (q.id = n.questionid) left outer join text_choice t on (q.id = t.questionid)"+
										"where q.id = ?";
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(SQLString);
		ps.setInt(1, id);
		return ps.executeQuery();
	}

	
	public static ResultSet findByQuizId(int id) throws SQLException{
		String SQLString = "SELECT * FROM question WHERE quizid = ?";
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(SQLString);
		ps.setInt(1, id);
		return ps.executeQuery();
	}
	
	public static ResultSet findByName(String name) throws SQLException {
		String SQLString = "SELECT * FROM question WHERE name = ?";
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(SQLString);
		ps.setString(1, name);
		return ps.executeQuery();
	}
}
